package main

import (
	"encoding/json"
	"errors"
	"fmt"
	"iot-base/common/cache"
	"iot-base/common/ctrltype"
	"iot-base/devc/common/loader"
	"net"
	"strconv"
	"strings"
	"time"
)

var serve map[string]string

func RegistServe() {
	loader.SubCtrlHandler(func(pCmd *ctrltype.CtrlCmd) (map[string]interface{}, error) {
		fmt.Println("ctrl cmd coming-----------------------------")

		_, okAll := pCmd.Params["ToAll"]
		if okAll {
			pCmd.ThingID = "ALL"

		}
		conns, err := net.DialTimeout("tcp", "127.0.0.1:37125", 3*time.Second)
		if err != nil {
			fmt.Println("conn server err:")
		}
		if conns != nil {
			defer conns.Close()
		}
		switch pCmd.ID {
		case "getPOEState":
			pThing, _ := cache.GetThing(pCmd.ThingID)
			//fmt.Println("found------------", pThing)
			if pThing != nil {
				ip := pThing.GetThingParam("poe", "")
				if ip == "" {
					return nil, errors.New("请配置POE地址")
				}
				fmt.Println("found------------", pThing, ip)
				return getPOEChannelInfo(ip), nil
			}
			return nil, errors.New("obj not found")
		case "enablePOE":
			pThing, _ := cache.GetThing(pCmd.ThingID)
			value, ok := pCmd.Params["no"].(string)
			if !ok {
				return nil, errors.New("no param is empty")
			}
			enable := pCmd.Params["state"].(string)
			//fmt.Println("found------------", pThing)
			if pThing != nil {
				ip := pThing.GetThingParam("poe", "")
				if ip == "" {
					return nil, errors.New("请配置POE地址")
				}
				//fmt.Println("found------------", pThing, ip)
				enablePOE(ip, value, enable == "enable")
				return nil, nil
			}
			return nil, errors.New("obj not found")

		case "lightness":
			value, ok := pCmd.Params["lightness"].(string)
			if !ok {
				fmt.Println("input value type err:")
			}
			Mux.Lock()
			lightswitch, err := json.Marshal(Light{"SetBRI", []DevLight{DevLight{boxistrue[pCmd.ThingID], value}}})
			Mux.Unlock()
			if err != nil {
				fmt.Println("json mashal err:", err)
				return nil, nil
			}
			WriteDt(lightswitch, conns)
			var reslight Reslight
			readb := ReadDt(conns)
			fmt.Println("readb:", string(readb))
			err = json.Unmarshal(readb, &reslight)
			fmt.Println("Reslight:", reslight)
			if err != nil {
				fmt.Println("Unmarshal err1:", err)
				return nil, nil
			}
			if len(reslight.Fail) == 0 {
				fmt.Println("control ok")
				return nil, nil
			} else {
				fmt.Println("control fail:", reslight.Fail)
				return nil, errors.New("control light switch fail")
			}
			return nil, nil
		case "light status":
			value, ok := pCmd.Params["light status"].(string)
			tmp := ReqAct{}
			tmp.Strobe = &value
			if !ok {
				fmt.Println("input value type err")
			}
			Mux.Lock()
			lightness_send, err := json.Marshal(ActPort{"ActPort", []DevAct{DevAct{boxistrue[pCmd.ThingID], tmp}}})
			Mux.Unlock()
			if err != nil {
				fmt.Println("json mashal err:", err)
				return nil, nil
			}
			fmt.Println("write:", string(lightness_send))
			WriteDt(lightness_send, conns)
			var reslight Reslight
			readb := ReadDt(conns)
			fmt.Println("readb:", string(readb))
			err = json.Unmarshal(readb, &reslight)
			fmt.Println("reslight:", reslight)
			if err != nil {
				fmt.Println("Unmarshal err:6", err)
				return nil, nil
			}
			if len(reslight.Fail) == 0 {
				fmt.Println("control ok")
				return nil, nil
			} else {
				fmt.Println("control fail:", reslight.Fail)
				return nil, errors.New("control lightness fail")
			}

		case "SetOutlet":
			Mux.Lock()
			Outlet_send, err := json.Marshal(SetOutlet{"SetOutlet", boxistrue[pCmd.ThingID], pCmd.Params["Board"].(string), pCmd.Params["Outlet"].(string), pCmd.Params["Act"].(string)})
			Mux.Unlock()
			if err != nil {
				fmt.Println("SetOutlet json mashal err:", err)
				return nil, nil
			}
			WriteDt(Outlet_send, conns)
			var resoutlet ResOutlet
			ret := ReadDt(conns)
			err = json.Unmarshal(ret, &resoutlet)
			if err != nil {
				fmt.Println("SetOutlet Unmarshal err2:", err, string(ret))
				erro := errors.New("SetOutlet Unmarshal err:" + err.Error())
				return nil, erro
			}
			if resoutlet.Act != "Ok" {
				fmt.Println("SetOutlet control fail-----------", resoutlet.Act, string(ret))
				erro := errors.New("SetOutlet control fail")
				return nil, erro
			}
			return nil, nil
		case "GetConf":
			outConf := map[string]interface{}{}
			Mux.Lock()
			Outlet_send, err := json.Marshal(GetConf{"GetConfig", []ConfDev{ConfDev{boxistrue[pCmd.ThingID]}}})
			Mux.Unlock()
			if err != nil {
				fmt.Println("json mashal err:", err)
				erro := errors.New("json mashal err:" + err.Error())
				return nil, erro
			}
			WriteDt(Outlet_send, conns)
			readbuff := ReadDt(conns)
			if len(readbuff) == 0 {

				return nil, errors.New("return nil from devprovider")
			}
			var resConf ResSet
			err = json.Unmarshal(readbuff, &resConf)
			if err != nil {
				fmt.Println("Unmarshal err3:", err)
				erro := errors.New("Unmarshal err:" + err.Error())
				return nil, erro
			}
			//fmt.Println("readbuff:", string(readbuff))

			if len(resConf.Dev) < 1 {
				erro := errors.New("设备无应答")
				return nil, erro
			}
			if resConf.Dev[0].Conect == "Offline" {
				erro := errors.New("设备不在线")
				return nil, erro
			}
			outConf["AC220 max"] = resConf.Dev[0].Scope.AC220.Max
			outConf["AC220 min"] = resConf.Dev[0].Scope.AC220.Min
			outConf["DC12 max"] = resConf.Dev[0].Scope.DC12.Max
			outConf["DC12 min"] = resConf.Dev[0].Scope.DC12.Min
			outConf["AC24 max"] = resConf.Dev[0].Scope.AC24.Max
			outConf["AC24 min"] = resConf.Dev[0].Scope.AC24.Min
			outConf["Tem max"] = resConf.Dev[0].Scope.Tem.Max
			outConf["Tem min"] = resConf.Dev[0].Scope.Tem.Min
			outConf["Hum max"] = resConf.Dev[0].Scope.Hum.Max
			outConf["Hum min"] = resConf.Dev[0].Scope.Hum.Min
			outConf["Heat start"] = resConf.Dev[0].Scope.Heat.Sta
			outConf["Heat stop"] = resConf.Dev[0].Scope.Heat.Sto
			outConf["Fan start"] = resConf.Dev[0].Scope.Fan.Sta
			outConf["Fan stop"] = resConf.Dev[0].Scope.Fan.Sto
			outConf["Fan Speed"] = resConf.Dev[0].Scope.Speed
			outConf["Acc slope"] = resConf.Dev[0].Scope.Acc.Slope
			outConf["Acc shock"] = resConf.Dev[0].Scope.Acc.Shock

			if resConf.Dev[0].Net.UDP != nil {
				outConf["trap ip"] = resConf.Dev[0].Net.UDP.IP
				outConf["trap port"] = resConf.Dev[0].Net.UDP.Port
			}
			if resConf.Dev[0].Net.TCP != nil {
				outConf["mac"] = resConf.Dev[0].Net.TCP.MAC
			}

			fmt.Println(outConf)
			return outConf, nil

		case "SetConf":
			SetConf := SetConfig{}
			SetConf.REQ = "SetConfig"
			dev := &SetScope{}
			udpIP, ok := pCmd.Params["trap ip"]
			if ok {
				pip, _ := udpIP.(string)
				dev.Net = &Netconf{UDP: &UdpConf{}}
				dev.Net.UDP.IP = &pip
				trapPort, okPort := pCmd.Params["trap port"]
				if okPort {
					pport, _ := trapPort.(string)
					dev.Net.UDP.Port = &pport
				}
			}

			var setScope = true
			onlyTrap, ok := pCmd.Params["onlyTrap"]
			if ok {
				if onlyTrap == "true" {
					setScope = false
				}
			}
			if setScope {
				scope := &Scopeconf{}

				scope.AC220.Max = pCmd.Params["AC220 max"].(string)
				scope.AC220.Min = pCmd.Params["AC220 min"].(string)
				scope.DC12.Max = pCmd.Params["DC12 max"].(string)
				scope.DC12.Min = pCmd.Params["DC12 min"].(string)
				scope.AC24.Max = pCmd.Params["AC24 max"].(string)
				scope.AC24.Min = pCmd.Params["AC24 min"].(string)
				scope.Tem.Max = pCmd.Params["Tem max"].(string)
				scope.Tem.Min = pCmd.Params["Tem min"].(string)
				scope.Hum.Max = pCmd.Params["Hum max"].(string)
				scope.Hum.Min = pCmd.Params["Hum min"].(string)
				scope.Heat.Sta = pCmd.Params["Heat start"].(string)
				scope.Heat.Sto = pCmd.Params["Heat stop"].(string)
				scope.Fan.Sta = pCmd.Params["Fan start"].(string)
				scope.Fan.Sto = pCmd.Params["Fan stop"].(string)
				scope.Speed = pCmd.Params["Fan Speed"].(string)
				scope.Acc.Slope = pCmd.Params["Acc slope"].(string)
				scope.Acc.Shock = pCmd.Params["Acc shock"].(string)

				dev.Scope = scope

			}
			SetConf.Dev = []SetScope{*dev}
			if pCmd.ThingID == "ALL" {
				SetConf.Dev[0].UID = "$UID$"
				SendDt, _ := json.Marshal(SetConf)
				if !setScope {
					pLinkage := &cache.ModelLinkage{
						ModelID:    "m_smartBoxForPolice",
						Trigger:    "online",
						Expression: SendDt,
						LType:      "setconf.trap",
					}
					pLinkage.SaveOrCreate()
				}
				//return nil, nil

				Mux.Lock()
				array := make([]string, 0, len(boxistrue))
				//var i = 0
				for k, v := range boxistrue {
					if v == "" {
						fmt.Println("error kv ", k)
						continue
					}
					array = append(array, v)
				}
				Mux.Unlock()
				go func() {
					conn, _ := net.DialTimeout("tcp", "127.0.0.1:37125", 3*time.Second)
					if conn != nil {
						defer conn.Close()
					}
					for _, v := range array {
						if v == "" {
							continue
						}
						SetConf.Dev[0].UID = v
						SendDt, err := json.Marshal(SetConf)

						if err != nil {
							fmt.Println("json mashal err:", err)
							return
						}
						fmt.Println("SendDt:", string(SendDt))
						err = WriteDt(SendDt, conn)
						if err == nil {
							var resConf ResConf
							readbuff := ReadDt(conn)
							err = json.Unmarshal(readbuff, &resConf)
							fmt.Println("result:", string(readbuff), err)

						} else {
							fmt.Println("send result:", err)

						}
					}

				}()

				return nil, nil

			}
			Mux.Lock()
			SetConf.Dev[0].UID = boxistrue[pCmd.ThingID]
			Mux.Unlock()
			SendDt, err := json.Marshal(SetConf)

			if err != nil {
				fmt.Println("json mashal err:", err)
				erro := errors.New("json mashal err:" + err.Error())
				return nil, erro
			}
			fmt.Println("SendDt:", string(SendDt))
			WriteDt(SendDt, conns)
			var resConf ResConf
			readbuff := ReadDt(conns)
			err = json.Unmarshal(readbuff, &resConf)
			fmt.Println("result:", string(readbuff))
			if err != nil {
				fmt.Println("Unmarshal err4:", err)
				erro := errors.New("Unmarshal err4:" + err.Error())
				return nil, erro
			}
			if len(resConf.Fail) != 0 {
				fmt.Println("Unmarshal err5:")
				erro := errors.New("Set fail")
				return nil, erro
			}
		case "ActSys":
			return actSys(pCmd.Params, pCmd.ThingID, conns)
		case "lock":
			value, ok := pCmd.Params["lock status"].(string)
			tmp := ReqAct{}
			tmp.Latch = &value
			if !ok {
				fmt.Println("input value type err:")
			}
			Mux.Lock()
			lightswitch, err := json.Marshal(ActPort{"ActPort", []DevAct{DevAct{boxistrue[pCmd.ThingID], tmp}}})
			Mux.Unlock()
			if err != nil {
				fmt.Println("json mashal err:", err)
				return nil, nil
			}
			WriteDt(lightswitch, conns)
			var resact ResAct
			readb := ReadDt(conns)
			fmt.Println("readb:", string(readb))
			err = json.Unmarshal(readb, &resact)
			if err != nil {
				fmt.Println("Unmarshal err8:", err)
				return nil, nil
			}
		case "set dot1x":
			user, _ := pCmd.Params["User"].(string)
			psw, _ := pCmd.Params["PSW"].(string)
			SetConf := SetConfig{}
			SetConf.REQ = "SetConfig"
			dev := &SetScope{}
			Mux.Lock()
			dev.UID = boxistrue[pCmd.ThingID]
			Mux.Unlock()
			var dot1x Dot1Xset = Dot1Xset{}
			dot1x.User = user
			dot1x.PSW = psw
			dev.Dot1X = &dot1x
			SetConf.Dev = []SetScope{*dev}
			SendDt, err := json.Marshal(SetConf)
			if err != nil {
				fmt.Println("json mashal err:", err)
				erro := errors.New("json mashal err:" + err.Error())
				return nil, erro
			}
			fmt.Println("SendDt:", string(SendDt))
			WriteDt(SendDt, conns)
			var resConf ResConf
			readbuff := ReadDt(conns)
			err = json.Unmarshal(readbuff, &resConf)
			fmt.Println("result:", string(readbuff))
			if err != nil {
				fmt.Println("Unmarshal err9:", err)
				erro := errors.New("Unmarshal err9:" + err.Error())
				return nil, erro
			}
			if len(resConf.Fail) != 0 {
				fmt.Println("Unmarshal err5:")
				erro := errors.New("set fail")
				return nil, erro
			}
			return nil, nil
		case "update":
			uid, _ := pCmd.Params["uid"].(string)
			path, _ := pCmd.Params["path"].(string)
			uids := strings.Split(uid, ",")
			var sendMsg FwUpdate
			sendMsg = FwUpdate{}
			sendMsg.REQ = "FwUpdate"
			sendMsg.FName = path
			sendMsg.Dev = make([]DevMsg, 1)
			for n, v := range uids {
				sendMsg.Dev[n] = DevMsg{
					v,
				}
			}
			SendDt, err := json.Marshal(sendMsg)
			if err != nil {
				fmt.Println("json mashal err:", err)
				erro := errors.New("json mashal err:" + err.Error())
				return nil, erro
			}
			fmt.Println("SendDt:", string(SendDt))
			WriteDt(SendDt, conns)
			var resFwUpdate ResFwUpdate
			readbuff := ReadDt(conns)
			err = json.Unmarshal(readbuff, &resFwUpdate)
			if err != nil {
				fmt.Println("Unmarshal err4:", err)
			}
			fmt.Println("result:", string(readbuff))
			return nil, nil
		case "SetSysMask":
			mask, _ := pCmd.Params["Mask"].(string)
			masks := strings.Split(mask, ",")
			var sysm SysM
			sysm = SysM{}
			if mask != "" {
				sysm.SysMask = make([]int, len(masks))
				for n, v := range masks {
					intv, _ := strconv.ParseInt(v, 10, 0)
					sysm.SysMask[n] = int(intv)
				}
			} else {
				sysm.SysMask = make([]int, 0)
			}
			SetConf := SysMaskConf{}
			SetConf.REQ = "SetConfig"

			if pCmd.ThingID == "ALL" {
				//SetConf.Dev = []DevMask{DevMask{"$UID$", sysm}}

				//SendDt, _ := json.Marshal(SetConf)

				// pLinkage := &cache.ModelLinkage{
				// 	ModelID:    "m_smartBoxForPolice",
				// 	Trigger:    "online",
				// 	Expression: SendDt,
				// 	LType:      "setconf.sysmask",
				// }
				//pLinkage.SaveOrCreate()

				Mux.Lock()
				for _, v := range boxistrue {
					SetConf.Dev = []DevMask{DevMask{v, sysm}}

					SendDt, err := json.Marshal(SetConf)
					if err != nil {
						fmt.Println("json mashal err:", err)
						erro := errors.New("json mashal err:" + err.Error())
						Mux.Unlock()
						return nil, erro
					}
					WriteDt(SendDt, conns)
					fmt.Println("SendDt all:", string(SendDt))
					var resConf ResConf
					readbuff := ReadDt(conns)
					err = json.Unmarshal(readbuff, &resConf)
					fmt.Println("result:", string(readbuff), err)

				}
				Mux.Unlock()
				return nil, nil

			}
			Mux.Lock()
			SetConf.Dev = []DevMask{DevMask{boxistrue[pCmd.ThingID], sysm}}
			Mux.Unlock()
			SendDt, err := json.Marshal(SetConf)
			if err != nil {
				fmt.Println("json mashal err:", err)
				erro := errors.New("json mashal err:" + err.Error())
				return nil, erro
			}
			fmt.Println("SendDt:", string(SendDt))
			WriteDt(SendDt, conns)

			var resConf ResConf
			readbuff := ReadDt(conns)
			err = json.Unmarshal(readbuff, &resConf)
			fmt.Println("result:", string(readbuff))
			if err != nil {
				fmt.Println("Unmarshal err4:", err)
				erro := errors.New("Unmarshal err4:" + err.Error())
				return nil, erro
			}
			if len(resConf.Fail) != 0 {
				fmt.Println("Unmarshal err5:", resConf.Fail)
				erro := errors.New("unmarshal err5: set fail")
				return nil, erro
			}
		case "GetSysMask":
			Mux.Lock()
			defer Mux.Unlock()
			return getSysMask(boxistrue[pCmd.ThingID])
		case "GetIPC":
			outIPC := map[string]interface{}{}
			Outlet_send, err := json.Marshal(GetConf{"GetConfig", []ConfDev{ConfDev{boxistrue[pCmd.ThingID]}}})
			if err != nil {
				fmt.Println("json mashal err:", err)
				erro := errors.New("json mashal err:" + err.Error())
				return nil, erro
			}
			WriteDt(Outlet_send, conns)
			readbuff := ReadDt(conns)
			fmt.Println("readbuff:", string(readbuff))
			var resConf ResSet
			err = json.Unmarshal(readbuff, &resConf)
			if err != nil {
				fmt.Println("Unmarshal err3:", err)
				erro := errors.New("Unmarshal err:" + err.Error())
				return nil, erro
			}
			if len(resConf.Dev) < 1 {
				erro := errors.New("设备无应答")
				return nil, erro
			}
			outIPC["ICMP"] = resConf.Dev[0].ICMP
			return outIPC, nil
		case "IPCSet":
			SetConf := SetConfig{}
			SetConf.REQ = "SetConfig"
			dev := &SetScope{}
			dev.ICMP = &ICMPset{}
			Cam, ok := pCmd.Params["Cam"].(string)
			if !ok {
				return nil, errors.New("Cam is err:" + Cam)
			}
			B, okB := pCmd.Params["B"].(string)
			if !okB {
				return nil, errors.New("B is err:" + B)
			}
			P, okP := pCmd.Params["P"].(string)
			if !okP {
				return nil, errors.New("P is err:" + P)
			}
			IP, okIP := pCmd.Params["IP"].(string)
			if !okIP {
				return nil, errors.New("IP is err:" + IP)
			}
			UID, okUID := pCmd.Params["UID"].(string)
			if !okUID {
				return nil, errors.New("UID is err:" + UID)
			}
			switch Cam {
			case "0":
				dev.ICMP.Cam0 = &CamSet{
					B,
					P,
					IP,
				}
			case "1":
				dev.ICMP.Cam1 = &CamSet{
					B,
					P,
					IP,
				}
			case "2":
				dev.ICMP.Cam2 = &CamSet{
					B,
					P,
					IP,
				}
			case "3":
				dev.ICMP.Cam3 = &CamSet{
					B,
					P,
					IP,
				}
			case "4":
				dev.ICMP.Cam4 = &CamSet{
					B,
					P,
					IP,
				}
			case "5":
				dev.ICMP.Cam5 = &CamSet{
					B,
					P,
					IP,
				}
			case "6":
				dev.ICMP.Cam6 = &CamSet{
					B,
					P,
					IP,
				}
			case "7":
				dev.ICMP.Cam7 = &CamSet{
					B,
					P,
					IP,
				}
			default:
				return nil, errors.New("Cam is err:" + Cam)
			}
			Mux.Lock()
			dev.UID = boxistrue[pCmd.ThingID]
			Mux.Unlock()
			SetConf.Dev = []SetScope{*dev}
			SendDt, err := json.Marshal(SetConf)
			if err != nil {
				fmt.Println("json mashal err:", err)
				erro := errors.New("json mashal err:" + err.Error())
				return nil, erro
			}
			fmt.Println("SendDt:", string(SendDt))
			WriteDt(SendDt, conns)
			var resConf ResConf
			readbuff := ReadDt(conns)
			err = json.Unmarshal(readbuff, &resConf)
			fmt.Println("result:", string(readbuff))
			if err != nil {
				fmt.Println("Unmarshal err4:", err)
				erro := errors.New("Unmarshal err4:" + err.Error())
				return nil, erro
			}
			if len(resConf.Fail) != 0 {
				fmt.Println("Unmarshal err5:", resConf.Fail)
				erro := errors.New("unmarshal err5: set fail ")
				return nil, erro
			}
			if resConf.Ok == nil && resConf.Fail == nil {
				fmt.Printf("res:%s,Dev uid:%s", resConf.RES, dev.UID)
				erro := errors.New("res:" + resConf.RES + ",Dev uid:" + dev.UID)
				return nil, erro
			}
			//key := "box." + pCmd.ThingID + "." + B + "." + P
			//value := UID
			//rc.Set(ctx, key, value, 0)
		}
		return nil, nil
	})
	//loader.SubCtrlHandler(func(pCmd *ctrltype.CtrlCmd) (map[string]interface{}, error){
	//
	//
	//	return nil,nil
	//})
}

func getSysMask(id string) (map[string]interface{}, error) {
	//fmt.Println("get id-------getSysMask-----", id)
	conns, err := net.DialTimeout("tcp", "127.0.0.1:37125", 3*time.Second)
	if err != nil {
		fmt.Println("conn server err:")
	}
	if conns != nil {
		defer conns.Close()
	}
	outConf := map[string]interface{}{}
	Outlet_send, err := json.Marshal(GetConf{"GetConfig", []ConfDev{ConfDev{id}}})
	if err != nil {
		fmt.Println("json mashal err:", err)
		erro := errors.New("json mashal err:" + err.Error())
		return nil, erro
	}
	WriteDt(Outlet_send, conns)

	//fmt.Println("sendbuff:", string(Outlet_send))
	readbuff := ReadDt(conns)
	//fmt.Println("readbuff:", string(readbuff))
	var resConf ResSet
	err = json.Unmarshal(readbuff, &resConf)
	if err != nil {
		fmt.Println("Unmarshal err3:", err)
		erro := errors.New("Unmarshal err:" + err.Error())
		return nil, erro
	}
	if len(resConf.Dev) < 1 {
		erro := errors.New("设备无应答")
		return nil, erro
	}
	/*
		tmpstr := ""
		if resConf.Dev[0].Sys.SysMask != nil {
			nv := resConf.Dev[0].Sys.SysMask
			for _, v := range *nv {
				if tmpstr != "" {
					tmpstr += ","
				}
				tmpstr += fmt.Sprint(v)
			}
		}*/
	outConf["AlarmShieldList"] = resConf.Dev[0].Sys.SysMask
	outConf["IOShiledList"] = resConf.Dev[0].Sys.IOMask
	//fmt.Println("alarm list -------------------------------------------------", outConf["AlarmShieldList"])
	return outConf, nil

}

func actSys(params map[string]interface{}, thingID string, conns net.Conn) (map[string]interface{}, error) {
	ActSys := ActSys{}
	ActSys.REQ = "ActSys"
	Mux.Lock()
	ActSys.Dev = []ActSysDev{ActSysDev{boxistrue[thingID], ActDev{}}}
	Mux.Unlock()
	fmt.Println("***************", params)
	tmp1, _ := params["SysReset"].(string)
	if tmp1 != "" {
		ActSys.Dev[0].Act.SysReset = &tmp1
	}
	tmp2, _ := params["RestoreConfig"].(string)
	if tmp2 != "" {
		ActSys.Dev[0].Act.RestoreConfig = &tmp2
	}
	tmp3, _ := params["LanReset"].(string)
	if tmp3 != "" {
		ActSys.Dev[0].Act.LanReset = &tmp3
	}
	tmp4, _ := params["ESwReset"].(string)
	if tmp4 != "" {
		ActSys.Dev[0].Act.ESwReset = &tmp4
	}
	tmp5, _ := params["SpdClear"].(string)
	if tmp5 != "" {
		ActSys.Dev[0].Act.SpdClear = &tmp5
	}
	SendDt, err := json.Marshal(ActSys)
	if err != nil {
		fmt.Println("json mashal err:", err)
		erro := errors.New("json mashal err:" + err.Error())
		return nil, erro
	}
	WriteDt(SendDt, conns)
	var resConf ResActSys
	readbuff := ReadDt(conns)
	err = json.Unmarshal(readbuff, &resConf)
	fmt.Println("result:", string(readbuff))
	if err != nil {
		fmt.Println("Unmarshal err6:", err)
		erro := errors.New("Unmarshal err:" + err.Error())
		return nil, erro
	}
	if len(resConf.Fail) != 0 {
		fmt.Println("cmd error-----------------")
		erro := errors.New("Set fail")
		return nil, erro
	}
	return nil, nil

}
